<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use App\Models\Invoice;
use App\Models\Order;
use Illuminate\Support\Facades\Auth;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{
    private $user_id;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {   
        $this->user_id = Auth::id();
        date_default_timezone_set('asia/shanghai');
    }
    //单个订单获取详情
    public function get($id)
    {   
        $detail = DB::table('order')->find($id);
        if (empty($detail)) 
        {
            return $this->respond('get_error');
        }
        $data['orderInfo'] = $detail;
        $data['invoiceInfo'] = [];
        $invoice = DB::table('invoice')->where('orderId',$detail->id)->first();
        $invoice && $data['invoiceInfo'] = $invoice;
        return $this->respond('get_ok', $data);
    }
    //所有订单获取详情
    public function all(Request $request)
    {   
        $order = new Order;
        $input = $request->input();
        $page = $this->page($request);
        $pageNum = $request->input('pageNum') ? $request->input('pageNum') : 1;
        $list = $order->getList($this->user_id,$input);
        $item = array_slice($list, $page['skip'], $page['take']);
        if(!count($list))
        {
            $data = array(['item'=>[]]);
            return $this->respond('get_ok', $data);
        }
        $count = count($list);
        $num = ceil($count/$pageNum);
        $data = array('pageNum' => $num, 'count' => $count, 'item' => $item);
        return $this->respond('get_ok', $data);
    }
    //所有订单导出excel
    public function export(Request $request)
    {   
        $order = new Order;
        $input = $request->input();
        $page = $this->page($request);
        $pageNum = $request->input('pageNum') ? $request->input('pageNum') : 1;
        $list = $order->getList($this->user_id,$input);
        if(!count($list))
        {
            return $this->respond('get_error');
        }
        $cellData = [];
        $i = 1;
        $cellData = [['版本id','版本名称','订单号','订单产品名称','订单价格','优惠价格','支付流水号','支付类型','支付状态','创建时间','支付时间']];
        $title = '订单记录_'.time();
        foreach($list as $key=>$val)
        {   
            $from = ( $val->type == 1 ) ? '微信' : '支付宝';
            $status = ( $val->status == 1 ) ? '未支付' : '已支付';
            $cellData[$i] = [$val->versionId,$val->name,$val->orderNo,$val->term,$val->price,$val->discount,$val->outTradeNo,$from,$status,date('Y年m月d日 H:i:s',$val->createdAt),date('Y年m月d日 H:i:s',$val->updatedAt) ];
            $i++;
        }   
        Excel::create($title,function($excel) use ($cellData)
        {
            $excel->sheet('Sheet', function($sheet) use ($cellData)
            {
                $sheet->rows($cellData);
            });
        })->store('xls','uploads/excel');
        $path['filepath'] = '/uploads/excel/'.$title.'.xls';
        return $this->respond('get_ok', $path);
    }
}
